library(nycflights13)
library(dplyr)
library(ggplot2)

by_dest <- group_by(flights, dest)
delay <- summarize(by_dest, 
                   count = n(),
                   dist = mean(distance, na.rm = T),
                   delay = mean(arr_delay, na.rm = T)
)
delay <- filter(delay, count > 20, dest != "HNL")

ggplot(delay, aes(x = dist, y = delay)) +
  geom_point(aes(size = count), alpha = 1/3) +
  geom_smooth(se = F)

delay <- group_by(flights, dest) %>% 
  summarize(count = n(),
            dist = mean(distance, na.rm = T),
            delay = mean(arr_delay, na.rm = T)) %>% 
  filter

batting <- as_tibble(Lahman::Batting)
batters <- batting %>% 
  group_by(playerID) %>% 
  summarize(ba = sum(H, na.rm = T) / sum(AB, na.rm = T),
            ab = sum(AB, na.rm = T))

batters %>% 
  filter(ab > 100) %>% 
  ggplot(aes(x = ab, y = ba)) +
    geom_point() +
    geom_smooth(se = F) # `geom_smooth()` using method = 'gam'

ggplot(diamonds) +
  geom_count(aes(x = cut, y = color))

ggplot(diamonds) +
  geom_bin2d(aes(x = carat, y = price))

ggplot(diamonds) +
  geom_hex(aes(x = carat, y = price))

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.1)))

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_boxplot(aes(group = cut_number(carat, 20)))
